<?php

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\oms;

use think\Model;

class Companies extends Model {

    protected $table = 'companies';
    protected $connection = ['database' => 'oms'];

    /**
     * 根据条件获取字段，并且判断是否开启缓存
     * @param type $field
     * @param type $condition
     * @param type $is_cache
     * @return type
     */
    public function getCompanyInfoByCondition($field = "*", $condition = [], $is_cache = false) {
        $single = $this;
        if ($condition) {
            $single = $single->where($condition);
        }
        return $single->field($field)->cache($is_cache, 3600)->find();
    }

    /**
     * 将Company和Mp关联查询
     */
    public function relateCompanyAndMp($companyCode) {
        $this->alias('company')->join('jsm.jj_mps as mps', 'mps.o_id = company.company_id', 'LEFT')
                ->where(['company.company_code' => "$companyCode"])->cache(true,3600)->select();
    }

    /**
     * 通过company_code获取sms_sign
     */
    public function getSmsSignByCompanyCode($companyCode) {
        $rs = $this->field('sms_sign')->where(['company_code' => $companyCode])->find()['sms_sign'];
        return $rs;
    }

    /**
     * 通过ID获取集团及门店设备接口
     * @param type $companyId
     */
    public function getCompanyOptometryDeviceByCompanyCode($companyCode) {
        $baseUrl = \think\Config::get('OSS_DOMAIN');
        $res = $this
                ->alias('company')
                ->field('cod.id cod_id, company.company_id, cod.name, cod.name_en, brand, CONCAT("' . $baseUrl . '",cod.logo) as logo, key,cod.type isSelect')
                ->join('company_optometry_device cod', 'cod.company_id = company.company_id', 'INNER')
                //->join('store_optometry_device sod', 'sod.cod_id = cod.id', 'INNER')
                ->where(['company.company_code' => $companyCode, 'cod.delete_time' => ['exp', 'is null']])
                ->select();
        return $res;
    }

}
